﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using monitor_2_DBwork;
using ServerLib.Entities;

namespace monitor_server_2_WPF.DeviceListParamPanels
{
    /// <summary>
    /// Логика взаимодействия для DeviceParamPanel.xaml
    /// </summary>
    public partial class DeviceParamPanel : UserControl
    {
        private string _station;
        private static readonly DeviceListTabControl.UpdateRegionTreeNodeDelegate UpdateRegionTreeNode = DeviceListTabControl.UpdateRegion;

        private DeviceParamPanel()
        {
            InitializeComponent();
            AcceptButton.Content = new MyDockPanel(Res.GetBitMapFromPng("accept_image_16"), Res.Rm.GetString("Accept"));
            DeleteButton.Content = new MyDockPanel(Res.GetBitMapFromPng("delete_image_16"), Res.Rm.GetString("Delete"));
            StatusLabel.Content = Res.Rm.GetString("Status") + ':';
            IpLabel.Content = Res.Rm.GetString("IPAddress");
            DeviceTitleLabel.Content = Res.Rm.GetString("Title");
            Infos.AddInfo("invalidIp", "Invalid Ip-adress");
            Infos.AddInfo("invalidTitle", "Title can not be empty");
            StatusBox.Content = "";
            IpBox.TextChanged += IpBox_OnTextChanged;
            //Infos.AddInfo("11", "1337info");
        }

        private static DeviceParamPanel _panel;

        public static UIElement GetPanel(DeviceTreeItem.DeviceInfo info, string stationName)
        {
            if(_panel == null)
                _panel = new DeviceParamPanel();
            _panel.HeadLabel.Content = info.Device;
            if (!string.IsNullOrEmpty(info.Title))
                _panel.DeviceTitleBox.Text = info.Title;
            else
            {
                _panel.DeviceTitleBox.Text = info.Device;
            }
            _panel.IpBox.Text = info.IpAddress;
            _panel.SetDeviceStatus(info.IsActive);
            _panel._station = stationName;

            return _panel;
        }

        private void SetDeviceStatus(bool active)
        {
            StatusBox.Content = Res.Rm.GetString(active ? "activateActiveBoxLabelText" : "activateInactiveBoxLabelText");
            StatusBox.IsChecked = active;
        }

        private void StatusBox_OnChecked(object sender, RoutedEventArgs e)
        {
            SetDeviceStatus(true);
        }

        private void StatusBox_OnUnchecked(object sender, RoutedEventArgs e)
        {
            SetDeviceStatus(false);
        }

        private void IpBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var ipbox = sender as TextBox;
            if (!Res.IpCheck(ipbox.Text))
            {
                AcceptButton.IsEnabled = false;
                Infos.ShowInfo("invalidIp");
            }
            else
            {
                AcceptButton.IsEnabled = true;
                Infos.HideInfo("invalidIp");
            }
        }

        private void AcceptButton_OnClick(object sender, RoutedEventArgs e)
        {

            if (DBwork.UpdateData("device",
                                  new string[]
                                      {
                                          _panel.HeadLabel.Content.ToString(), _panel._station, _panel.IpBox.Text,
                                          _panel.StatusBox.IsChecked.ToString(), _panel.DeviceTitleBox.Text
                                      }))
            {
                MessageBox.Show(Res.Rm.GetString("Complete"));
            }
            else
            {
                MessageBox.Show("Record can not be updated.", Res.Rm.GetString("Error"), MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void DeleteButton_OnClick(object sender, RoutedEventArgs e)
        {
            var res = MessageBox.Show(Res.Rm.GetString("DeleteElementDialog") + "?\r\n" + HeadLabel.Content + " - " + _station, Res.Rm.GetString("Warning"), MessageBoxButton.YesNo,
                MessageBoxImage.Question);
            if (res != MessageBoxResult.Yes)
                return;
            if (DBwork.DeleteData("device", HeadLabel.Content.ToString(), _station))
            {
                MessageBox.Show(Res.Rm.GetString("Complete"));
                UpdateRegionTreeNode();
            }
            else
            {
                MessageBox.Show("Record can not be updated.", Res.Rm.GetString("Error"), MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void DeviceTitleBox_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var tb = sender as TextBox;
            if (string.IsNullOrEmpty(tb.Text))
            {
                Infos.ShowInfo("invalidTitle");
            }
            else
            {
                Infos.HideInfo("invalidTitle");
            }
        }
    }
}
